package cn.anc.aonicardv.util.ui;

import android.text.TextUtils;

import cn.anc.aonicardv.util.StringUtils;

public class IpUtil {
    private static String[] IPHeader = {"192.168.42.", "192.168.25."};

    //判断是不是海思芯片
    public static boolean isHuaweiChip(String model) {
        if (!StringUtils.isEmptyOrNull(model)){
        String hisiModel = model.toLowerCase().trim();
        if (hisiModel.contains("hisi")){
            return true;
        }
        }
        return false;

    }


    public static String getContainsIp(String ip) {
        for (int i = 0; i < IPHeader.length; i++) {
            if (ip.contains(IPHeader[i])) {
                return IPHeader[i];
            }
        }
        return null;
    }

    public static String getOtaIp(String path) {
        String mIp = "";
        if (!TextUtils.isEmpty(path) && !TextUtils.isEmpty(getOtaSubPath(path))) {
            try {
                String[] temp = getOtaSubPath(path).split("/");
                String tmp = temp[0];
                String[] temp1 = tmp.split(":");
                mIp = temp1[0];
            } catch (Exception e) {
            }

        }
        return mIp;
    }

    public static int getOtaPort(String path) {
        int mPort = 80;
        if (!TextUtils.isEmpty(path) && !TextUtils.isEmpty(getOtaSubPath(path))) {
            try {
                String[] temp = getOtaSubPath(path).split("/");
                String tmp = temp[0];
                String[] temp1 = tmp.split(":");
                mPort = Integer.parseInt(temp1[1]);
            } catch (Exception e) {
            }

        }
        return mPort;
    }

    public static String getOtaUploadUri(String path) {
        if (TextUtils.isEmpty(path) || TextUtils.isEmpty(getOtaSubPath(path))) {
            return null;
        }
        String mUploadUri = null;
        try {
            String[] temp = getOtaSubPath(path).split("/");
            if (temp.length > 1) {
                for (int i = 0; i < temp.length; i++) {
                    if (i > 0) {
                        if (TextUtils.isEmpty(mUploadUri))
                            mUploadUri = "/" + temp[i];
                        else
                            mUploadUri = mUploadUri + "/" + temp[i];
                    }
                }
                if (!mUploadUri.endsWith("&md5=")) {
                    mUploadUri = mUploadUri + "&md5=";
                }
            }
        } catch (Exception e) {
        }

        return mUploadUri;
    }

    public static String getMstarOtaUploadUri(String path) {
        if (TextUtils.isEmpty(path) || TextUtils.isEmpty(getOtaSubPath(path))) {
            return null;
        }
        String mUploadUri = null;
        try {
            String[] temp = getOtaSubPath(path).split("/");
            if (temp.length > 1) {
                for (int i = 0; i < temp.length; i++) {
                    if (i > 0) {
                        if (TextUtils.isEmpty(mUploadUri))
                            mUploadUri = "/" + temp[i];
                        else
                            mUploadUri = mUploadUri + "/" + temp[i];
                    }
                }

            }
        } catch (Exception e) {
        }

        return mUploadUri;
    }


    public static String getOtaSubPath(String path) {
        if (path.startsWith("http://")) {
            path = path.substring("http://".length());
            if (path.contains("/")) {
                return path;
            }
        }
        return null;
    }


}
